// +----------------------------------------------------------------------
// | BossHongshi [鸿时互娱管理系统]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.hongshihuyu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: wgh
// +----------------------------------------------------------------------

module.exports = class extends think.cmswing.admin {
  /**
     * index action
     * @return {Promise} []
     */
  constructor(ctx) {
    super(ctx); // 调用父级的 constructor 方法，并把 ctx 传递进去
    // 其他额外的操作
  }

  /**
     * 数据统计
     * @returns {*}
     */

  async indexAction() {
	  //page
	
	const data ={
			"count":0,
			"totalPages":0,
			"pageSize":10,
			"currentPage":this.get('page') || 1,
	};
	let players = this.mongoose('mj_players', 'mongoose'); // use `mongoose` adapter type
	let count  = await players.count({}).lean().exec();
	console.log("count -----------:"+count);
	data.count = count;
	data.totalPages =Math.floor((count - 1) / 10 + 1);
	console.log("totalPages -----------:"+data.totalPages);
	//pageCount
	let start = ((this.get('page') || 1)-1)*10;
	let playerdata  = await players.find({}).skip(start).limit(10).sort({'create_time':-1}).lean().exec();
	console.log("playerdata -----------:"+JSON.stringify(playerdata));
	playerdata.forEach(v => {
	      v.last_login_time = v.last_login_time*1000;
	      v.create_time = v.create_time*1000;
	});
	
	data.data =playerdata;
    const html = this.pagination(data);
    console.log("html -----------:"+JSON.stringify(html));
    this.assign('pagerData', html); // 分页展示使用
    this.assign('list', data.data);
    this.meta_title = '玩家列表';
    return this.display();
  }
  
  async statAction() {
  	  /**
  	  if (!think.isEmpty(this.get('username'))) {
      map.username = ['like', '%' + this.get('username') + '%'];
      }
      */

      const start_date = this.get('startdate') || null;
      const end_date = this.get('enddate') || null;

      console.log("startdate -----------:"+start_date);
      console.log("enddate -----------:"+end_date);


  	  let today_s_time = new Date(GetDateStr(0) + ' ' + '00:00:00').getTime();
	    let today_e_time = new Date(GetDateStr(0) + ' ' + '23:59:59').getTime();
	  //今日充值
      const todayrechangemap = {
      	type:4
      };
      todayrechangemap.time = {'>=': today_s_time, '<=': today_e_time};
      const today_rechange = await this.model('balance_log').where(todayrechangemap).sum('amount');
      console.log("today_rechange -----------:"+today_rechange);
      this.assign('today_rechange', today_rechange);
      
      //今日总上分
      const todaycoinchargemap = {
      	action:'coinCharge'
      };
      todaycoinchargemap.actionTs = {'>=': today_s_time, '<=': today_e_time};
      const today_coinchange = await this.model('slot_recharge').where(todaycoinchargemap).sum('gainChip');
      console.log("today_coinchange -----------:"+today_coinchange);
      this.assign('today_coinchange', today_coinchange);
      
      //今日总下分
      const todaycoinreturnmap = {
      	action:'coinReturn'
      };
      todaycoinreturnmap.actionTs = {'>=': today_s_time, '<=': today_e_time};
      const today_coinreturn = await this.model('slot_recharge').where(todaycoinreturnmap).sum('gainChip');
      console.log("today_coinreturn -----------:"+today_coinreturn);
      this.assign('today_coinreturn', today_coinreturn);
      
      //今日新增代理数
      const todaynewagentmap = {
	    		'status': ['>', -1],
	    		is_admin:0
	    	};
	   
	   todaynewagentmap.reg_time = {'>=': today_s_time, '<=':today_e_time };
	   const today_newagent = await this.model('member').where(todaynewagentmap).count();
       console.log("today_newagent -----------:"+today_newagent);
      this.assign('today_newagent', today_newagent);

      //总充值
      const totalrechangemap = {
      	type:4
      };
      const total_rechange = await this.model('balance_log').where(totalrechangemap).sum('amount');
      console.log("total_rechange -----------:"+total_rechange);
      this.assign('total_rechange', total_rechange);

      //总上分
      const totalcoinchargemap = {
      	action:'coinCharge'
      };
      const total_coinchange = await this.model('slot_recharge').where(totalcoinchargemap).sum('gainChip');
      console.log("total_coinchange -----------:"+total_coinchange);
      this.assign('total_coinchange', total_coinchange);
      
      //总下分
      const totalcoinreturnmap = {
      	action:'coinReturn'
      };
      const total_coinreturn = await this.model('slot_recharge').where(totalcoinreturnmap).sum('gainChip');
      console.log("total_coinreturn -----------:"+total_coinreturn);
      this.assign('total_coinreturn', total_coinreturn);


      let yesterday_s_time = new Date(GetDateStr(-1) + ' ' + '00:00:00').getTime();
	  let yesterday_e_time = new Date(GetDateStr(-1) + ' ' + '23:59:59').getTime();
      
      //昨日总上分
      const yesterdaycoinchargemap = {
      	action:'coinCharge'
      };
      yesterdaycoinchargemap.actionTs = {'>=': yesterday_s_time, '<=': yesterday_e_time};
      const yesterday_coinchange = await this.model('slot_recharge').where(yesterdaycoinchargemap).sum('gainChip');
      console.log("yesterday_coinchange -----------:"+yesterday_coinchange);
      this.assign('yesterday_coinchange', yesterday_coinchange);
      
      //昨日总下分
      const yesterdaycoinreturnmap = {
      	action:'coinReturn'
      };
      yesterdaycoinreturnmap.actionTs = {'>=': yesterday_s_time, '<=': yesterday_e_time};
      const yesterday_coinreturn = await this.model('slot_recharge').where(yesterdaycoinreturnmap).sum('gainChip');
      console.log("yesterday_coinreturn -----------:"+yesterday_coinreturn);
      this.assign('yesterday_coinreturn', yesterday_coinreturn);

      //代理总数
      const totalagentmap = {
	    		'status': ['>', -1],
	    		is_admin:0
	    	};
	  
	   const total_agent = await this.model('member').where(totalagentmap).count();
       console.log("total_agent -----------:"+total_agent);
      this.assign('total_agent', total_agent);

      //设备总数
	  const total_device = await this.model('member_device').where({}).count();
      console.log("total_device -----------:"+total_device);
      this.assign('total_device', total_device);


     //活跃设备

	  const activedevicemap = {
	    	};
	  activedevicemap.actionTs = {'>=': today_s_time, '<=':today_e_time };
      const active_device_list = await this.model('slot_msg').where(activedevicemap).distinct('did').select();
      const active_device = active_device_list.length;

      console.log("active_device -----------:"+active_device);
      this.assign('active_device', active_device);

      const datastatmap = {};
	    if (!think.isEmpty(start_date)&&!think.isEmpty(end_date)) {
	      datastatmap.stat_date_str = {'>=':start_date, '<=': end_date};
	    }
	    let totalagentcount=0;
      let totaldevicecount=0;
      const data = await this.model('data_stat').where(datastatmap).page(this.get('page')|| 1, 10).order('stat_date_str DESC').countSelect();
      
      if(data.data.length>0){
         totalagentcount = data.data[0].total_agent_count;
         totaldevicecount = data.data[0].total_device_count;
      }
      /**
      for (const val of data.data) {
        totalagentcount = val.total_agent_count;
        totaldevicecount = val.total_device_count;
        break;
      }
      */

      const html = this.pagination(data);
      this.assign('pagerData', html);
      this.assign('count', data.count);
      this.assign('list', data.data);
      
      
      this.assign('totalagentcount', totalagentcount);
      this.assign('totaldevicecount', totaldevicecount);
      
      const totalnewagentcount = await this.model('data_stat').where(datastatmap).sum('new_agent_count');
      this.assign('totalnewagentcount', totalnewagentcount);

      const totalnewdevicecount = await this.model('data_stat').where(datastatmap).sum('new_device_count');
      this.assign('totalnewagentcount', totalnewagentcount);

      this.assign('totalnewdevicecount', totalnewdevicecount);
      //this.assign('totalCoinAfter', totalCoinAfter);

      this.assign('start_date', start_date);
      this.assign('end_date', end_date);

	    this.meta_title = '数据统计';
	    return this.display();
  }

  async dealAction() {
  	  const start_date = this.get('startdate') || null;
      const end_date = this.get('enddate') || null;
      const proxy_id = this.get('proxyid') || null;
      const device_id = this.get('deviceid') || null;

      console.log("startdate -----------:"+start_date);
      console.log("enddate -----------:"+end_date);
      console.log("proxy_id -----------:"+proxy_id);
      console.log("device_id -----------:"+device_id);

      let sql = `SELECT SUM(a.\`afterCoin\`) as totalcoin FROM \`cmswing_slot_recharge\` as a JOIN(
                SELECT \`uid\`,
                MAX(\`actionTs\`) actionTs
                FROM \`cmswing_slot_recharge\` where 1=1 `;

      const datadealmap = {};
	    if (!think.isEmpty(start_date)&&!think.isEmpty(end_date)) {
	  	  let start_time = new Date(start_date).getTime();
	  	  let end_time = new Date(end_date).getTime();
	      datadealmap.actionTs = {'>=':start_time, '<=': end_time};
        sql +=` AND \`actionTs\` >= `+start_time + ` AND \`actionTs\` <= `+end_time;
	    }

	    if (!think.isEmpty(proxy_id)) {
        const member = await this.model('member').where({proxy_id:proxy_id}).find();
        if(!think.isEmpty(member)){
           datadealmap.uid = member.id;
           sql +=` AND \`uid\` = `+member.id ;
        }
	    }
      if (!think.isEmpty(device_id)) {
        const device = await this.model('member_device').where({identity:device_id}).find();
        if(!think.isEmpty(device)){
          datadealmap.did = device.id;
          sql +=` AND \`did\` = `+device.id ;
        }
      }
      console.log("datadealmap -----------:"+JSON.stringify(datadealmap));
      
      let totalCoinCharge=0;
      let totalCoinReturn=0;
      let totalAfterCoin=0;
     

      const data = await this.model('slot_recharge').where(datadealmap).page(this.get('page')|| 1, 10).order('actionTs DESC').countSelect();
      //console.log("deal data -----------:"+JSON.stringify(data));

      const html = this.pagination(data);
      
      for (const val of data.data) {
      	const device = await this.model('member_device').find(val.did);
      	const member = await this.model('member').find(val.uid);
	      val.nickname = device.nickname;
        val.identity = device.identity;
        val.username = device.username;
        val.mobile = member.mobile;
        val.proxy_id = member.proxy_id;
        
        /**
        if(val.action == 'coinCharge'){
        	totalCoinCharge+=Number(val.gainChip);
        }
        if(val.action == 'coinReturn'){
        	totalCoinReturn+=Number(val.gainChip);
        }
        */
        //totalCoinAfter+=Number(val.afterCoin);
      }

      datadealmap.action = 'coinCharge';
      totalCoinCharge = await this.model('slot_recharge').where(datadealmap).sum('gainChip');

      datadealmap.action = 'coinReturn';
      totalCoinReturn = await this.model('slot_recharge').where(datadealmap).sum('gainChip');

      console.log("totalCoinCharge -----------:"+totalCoinCharge);
      console.log("totalCoinReturn -----------:"+totalCoinReturn);
      

      /**
      let sql = `SELECT SUM(a.\`afterCoin\`) as totalcoin FROM \`cmswing_slot_recharge\` as a JOIN(
                SELECT \`uid\`,
                MAX(\`id\`) id
                FROM \`cmswing_slot_recharge\` GROUP BY \`uid\`
                ) as b
                ON a.\`uid\`=b.\`uid\` and a.\`id\`=b.\`id\`;`;
      */

      sql +=` GROUP BY \`uid\`
                ) as b
                ON a.\`uid\`=b.\`uid\` and a.\`actionTs\`=b.\`actionTs\` ;`;

      console.log("deal sql -----------:"+sql);

      const res = await this.model('mysql').query(sql);
      if(!think.isEmpty(res)){
         console.log("res[0].totalcoin 11-----------:"+res[0].totalcoin); 
         totalAfterCoin = think.isEmpty(res[0].totalcoin)?0:res[0].totalcoin;   
      }
      console.log("totalAfterCoin 11-----------:"+totalAfterCoin);

      this.assign('totalCoinCharge', totalCoinCharge);
      this.assign('totalCoinReturn', totalCoinReturn);
      this.assign('totalAfterCoin', totalAfterCoin);

      this.assign('start_date', start_date);
      this.assign('end_date', end_date);
      this.assign('proxy_id', proxy_id);
      this.assign('device_id', device_id);

      this.assign('pagerData', html);
      this.assign('count', data.count);
      this.assign('list', data.data);
	  
	  this.meta_title = '交易统计';
	  return this.display();
  }

    /**
     * datacreate
     * 数据生成
     * @returns {Promise|*}
     */
  async datacreateAction() {
    var array1 = ["2019-01-20","2019-01-21","2019-01-22","2019-01-23","2019-01-24","2019-01-25"];
    
    /**
    var array2 = ["2019-01-01","2019-01-02","2019-01-03","2019-01-04","2019-01-05",
                  "2019-01-06","2019-01-07","2019-01-08","2019-01-09","2019-01-10",
                  "2019-01-11","2019-01-12","2019-01-13","2019-01-14","2019-01-15",
                  "2019-01-16","2019-01-17","2019-01-18","2019-01-19","2019-01-20"];
    */
    var array2 = ["2019-01-27"];
    // console.log(id);
    for (var i = 0; i <array2.length; i++) {
      var index = Math.floor((Math.random()*array1.length)); 
      console.log("index -----------:"+index);
      var datatime = array1[index];
      let start_time = new Date(datatime + ' ' + '00:00:00').getTime();
      let end_time = new Date(datatime + ' ' + '23:59:59').getTime();
      const datamap = {};
      datamap.actionTs = {'>=': start_time, '<=': end_time};
      const datalist = await this.model('slot_recharge').where(datamap).select();

      var datalistindex = Math.floor((Math.random()*datalist.length)); 

      for (var k = 0; k < datalistindex; k++) {
        var val = datalist[k];
        console.log("val.actionTs -----------:"+val.actionTs);
        var actionTs= time_format(val.actionTs);
        console.log("actionTs 2-----------:"+actionTs);
        //actionTs 2-----------:2019-01-25 16:58:18
        var actionTsStr = actionTs.toString();
        var hissubstr = actionTsStr.substring(11,actionTsStr.length);
        console.log("hissubstr -----------:"+hissubstr);

        let newactionTs = new Date(array2[i]+ ' ' + hissubstr).getTime();
        
        console.log("newactionTs 1-----------:"+newactionTs);
        console.log("newactionTs 2-----------:"+time_format(newactionTs));
        
        const slot_recharge = {
                'did':val.did,
                'uid': val.uid, //代理ID
                'module': val.module, //
                'action': val.action, //
                'gainChip': val.gainChip, //
                'actionTs': newactionTs,
                'beforeChip':val.beforeChip, //
                'afterChip': val.afterChip, //
                'afterCoin':val.afterCoin,
                'actionIndex': val.actionIndex
              };
        await this.model('slot_recharge').add(slot_recharge);
      }
      
      /**
      for (const val of datalist) {
        
        console.log("val.actionTs -----------:"+val.actionTs);
        var actionTs= time_format(val.actionTs);
        console.log("actionTs 2-----------:"+actionTs);
        //actionTs 2-----------:2019-01-25 16:58:18
        var actionTsStr = actionTs.toString();
        var hissubstr = actionTsStr.substring(11,actionTsStr.length);
        console.log("hissubstr -----------:"+hissubstr);

        let newactionTs = new Date(array2[i]+ ' ' + hissubstr).getTime();
        
        console.log("newactionTs 1-----------:"+newactionTs);
        console.log("newactionTs 2-----------:"+time_format(newactionTs));
        
        const slot_recharge = {
                'did':val.did,
                'uid': val.uid, //代理ID
                'module': val.module, //
                'action': val.action, //
                'gainChip': val.gainChip, //
                'actionTs': newactionTs,
                'beforeChip':val.beforeChip, //
                'afterChip': val.afterChip, //
                'afterCoin':val.afterCoin,
                'actionIndex': val.actionIndex
              };
        await this.model('slot_recharge').add(slot_recharge);
        
      }
      */
    }
    return this.success({name: '生成成功！'});
    
  }

    //数据统计
  async datastatAction() {
      
      console.log('数据统计处理流程 -- 开始!');
      //var datestr=GetDateStr(-1);

      var array2 = ["2019-01-01","2019-01-02","2019-01-03","2019-01-04","2019-01-05",
                  "2019-01-06","2019-01-07","2019-01-08","2019-01-09","2019-01-10",
                  "2019-01-11","2019-01-12","2019-01-13","2019-01-14","2019-01-15",
                  "2019-01-16","2019-01-17","2019-01-18"];
      
      for (var i = 0; i <array2.length; i++) {
          let datestr =  array2[i];
          let  data_stat_obj = await this.model('data_stat').where({stat_date_str: datestr}).find();

         if(think.isEmpty(data_stat_obj)){
         console.log(datestr+'日数据统计开始!');
         
         let yesterday_s_time = new Date(datestr + ' ' + '00:00:00').getTime();
         let yesterday_e_time = new Date(datestr + ' ' + '23:59:59').getTime();
        // 新增代理
        const yesterdaynewagentmap = {
          'status': ['>', -1],
          is_admin:0
        };
     
        yesterdaynewagentmap.reg_time = {'>=': yesterday_s_time, '<=':yesterday_e_time };
        const new_agent_count = await this.model('member').where(yesterdaynewagentmap).count();

        //代理总数
        const totalagentmap = {
          'status': ['>', -1],
          is_admin:0
        };
     totalagentmap.reg_time = {'<=':yesterday_e_time };
     const total_agent_count = await this.model('member').where(totalagentmap).count();
       
       //活跃代理数
        const activeagentmap = {
          'status': ['>', -1],
          is_admin:0
        };
     activeagentmap.last_login_time = {'>=': yesterday_s_time, '<=':yesterday_e_time };
     const active_agent_count = await this.model('member').where(totalagentmap).count();
        
        //新增设备
      const newdevicemap = {};
      newdevicemap.createTime = {'>=': yesterday_s_time, '<=':yesterday_e_time };
    const new_device_count = await this.model('member_device').where(newdevicemap).count();

     //设备总数
    const totaldevicemap = {};
      totaldevicemap.createTime = {'<=':yesterday_e_time };
    const total_device_count = await this.model('member_device').where(totaldevicemap).count();

         //活跃设备
      const activedevicemap = {
        };
    activedevicemap.actionTs = {'>=': yesterday_s_time, '<=':yesterday_e_time };
      const active_device = await this.model('slot_msg').where(activedevicemap).distinct('did').select();
      const active_device_count = active_device.length;

      //总上分
      const totalcoinchargemap = {
        action:'coinCharge'
      };
      totalcoinchargemap.actionTs = {'>=': yesterday_s_time, '<=':yesterday_e_time };
      const total_coinchange_count = await this.model('slot_recharge').where(totalcoinchargemap).sum('gainChip');
      
      //总下分
      const totalcoinreturnmap = {
        action:'coinReturn'
      };
      totalcoinreturnmap.actionTs = {'>=': yesterday_s_time, '<=':yesterday_e_time };
      const total_coinreturn_count = await this.model('slot_recharge').where(totalcoinreturnmap).sum('gainChip');

      //代理账户余额
       const accountcoincountmap = {
          'status': ['>', -1],
          is_admin:0
      };
     accountcoincountmap.reg_time = {'<=':yesterday_e_time };
     const account_coin_count = await this.model('member').where(accountcoincountmap).sum('coin_count');
     
     const datastat = {
                                stat_date_str:datestr,
                                new_agent_count:new_agent_count,
                                total_agent_count:total_agent_count,
                                active_agent_count:active_agent_count,
                                new_device_count:new_device_count,
                                total_device_count:total_device_count,
                                active_device_count:active_device_count,
                                            total_coinchange_count:total_coinchange_count,
                                            total_coinreturn_count:total_coinreturn_count,
                                            account_coin_count:account_coin_count,
                                otp_time: new Date().valueOf()
     };
      await this.model('data_stat').add(datastat);
      }else{
        console.log(datestr+'日数据存在，统计不开始!');
      }
      }
      //var date = new Date(datestr);
      
      
      console.log('数据统计处理流程  -- 结束!');
  }

      //数据统计更新
  async datastatupdateAction() {
      
      console.log('数据统计处理流程 -- 开始!');
      //var datestr=GetDateStr(-1);

      var array2 = [{"id":"2019-01-20","new_agent_count":0,"total_agent_count":12,"active_agent_count":1,"new_device_count":0,"total_device_count":18,"active_device_count":3},
{"id":"2019-01-19","new_agent_count":2,"total_agent_count":12,"active_agent_count":5,"new_device_count":2,"total_device_count":18,"active_device_count":6},
{"id":"2019-01-18","new_agent_count":1,"total_agent_count":10,"active_agent_count":2,"new_device_count":1,"total_device_count":16,"active_device_count":5},
{"id":"2019-01-17","new_agent_count":0,"total_agent_count":9,"active_agent_count":1,"new_device_count":0,"total_device_count":15,"active_device_count":3},
{"id":"2019-01-16","new_agent_count":0,"total_agent_count":9,"active_agent_count":1,"new_device_count":0,"total_device_count":15,"active_device_count":3},
{"id":"2019-01-15","new_agent_count":0,"total_agent_count":9,"active_agent_count":2,"new_device_count":1,"total_device_count":15,"active_device_count":5},
{"id":"2019-01-14","new_agent_count":1,"total_agent_count":9,"active_agent_count":2,"new_device_count":0,"total_device_count":14,"active_device_count":2},
{"id":"2019-01-13","new_agent_count":1,"total_agent_count":8,"active_agent_count":1,"new_device_count":1,"total_device_count":14,"active_device_count":4},
{"id":"2019-01-12","new_agent_count":0,"total_agent_count":7,"active_agent_count":2,"new_device_count":1,"total_device_count":13,"active_device_count":3},
{"id":"2019-01-11","new_agent_count":1,"total_agent_count":7,"active_agent_count":3,"new_device_count":2,"total_device_count":12,"active_device_count":5},
{"id":"2019-01-10","new_agent_count":0,"total_agent_count":6,"active_agent_count":4,"new_device_count":1,"total_device_count":10,"active_device_count":3},
{"id":"2019-01-09","new_agent_count":1,"total_agent_count":6,"active_agent_count":3,"new_device_count":1,"total_device_count":9,"active_device_count":4},
{"id":"2019-01-08","new_agent_count":1,"total_agent_count":5,"active_agent_count":4,"new_device_count":2,"total_device_count":8,"active_device_count":3},
{"id":"2019-01-07","new_agent_count":0,"total_agent_count":4,"active_agent_count":2,"new_device_count":1,"total_device_count":6,"active_device_count":3},
{"id":"2019-01-06","new_agent_count":0,"total_agent_count":4,"active_agent_count":2,"new_device_count":0,"total_device_count":5,"active_device_count":3},
{"id":"2019-01-05","new_agent_count":0,"total_agent_count":4,"active_agent_count":3,"new_device_count":0,"total_device_count":5,"active_device_count":1},
{"id":"2019-01-04","new_agent_count":1,"total_agent_count":4,"active_agent_count":2,"new_device_count":1,"total_device_count":5,"active_device_count":2},
{"id":"2019-01-03","new_agent_count":0,"total_agent_count":3,"active_agent_count":4,"new_device_count":1,"total_device_count":4,"active_device_count":1},
{"id":"2019-01-02","new_agent_count":0,"total_agent_count":3,"active_agent_count":1,"new_device_count":0,"total_device_count":3,"active_device_count":1},
{"id":"2019-01-01","new_agent_count":1,"total_agent_count":3,"active_agent_count":0,"new_device_count":1,"total_device_count":3,"active_device_count":0}];
    for (var i = 0; i <array2.length; i++) {
       console.log("array2 -----------"+i+'----:'+JSON.stringify(array2[i]));
      //var obj = JSON.parse(array2[i]);
      var obj = array2[i];
      let  data_stat_obj = await this.model('data_stat').where({stat_date_str: obj.id}).find();
      data_stat_obj.new_agent_count = obj.new_agent_count;
      data_stat_obj.total_agent_count = obj.total_agent_count;
      data_stat_obj.active_agent_count = obj.active_agent_count;
      data_stat_obj.new_device_count = obj.new_device_count;
      data_stat_obj.total_device_count = obj.total_device_count;
      data_stat_obj.active_device_count = obj.active_device_count;
          
      
      await this.model('data_stat').where({id:data_stat_obj.id}).update(data_stat_obj);
      
      }

      
      console.log('数据统计处理流程  -- 结束!');
  }

        //数据统计更新
  async datastatupdatecoinAction() {
      
      console.log('数据统计处理流程 -- 开始!');
      //var datestr=GetDateStr(-1);
      var array2 = ["2019-01-08","2019-01-09","2019-01-10",
                  "2019-01-11","2019-01-12","2019-01-13","2019-01-14","2019-01-15",
                  "2019-01-16","2019-01-17","2019-01-18","2019-01-19","2019-01-20",
                  "2019-01-21","2019-01-22","2019-01-23","2019-01-24","2019-01-25","2019-01-26"];

      for (var i = 0; i <array2.length; i++) {
       console.log("array2 -----------"+i+'----:'+array2[i]);

       var qianyitian = addDate(array2[i],-1);
       console.log("qianyitian -----------"+i+'----:'+qianyitian);
       const datadealmap = {};
       let start_time = new Date(array2[i]+ ' ' + '00:00:00').getTime();
       let end_time = new Date(array2[i] + ' ' + '23:59:59').getTime();
       datadealmap.actionTs = {'>=':start_time, '<=': end_time};
        
       datadealmap.action = 'coinCharge';
       let total_coinchange_count = await this.model('slot_recharge').where(datadealmap).sum('gainChip');
       console.log("total_coinchange_count ---------------:"+total_coinchange_count);

       datadealmap.action = 'coinReturn';
       let total_coinreturn_count = await this.model('slot_recharge').where(datadealmap).sum('gainChip');
       console.log("total_coinreturn_count ---------------:"+total_coinreturn_count);

       let  qian_data_stat_obj = await this.model('data_stat').where({stat_date_str:qianyitian}).find();
       console.log("qian_data_stat_obj ---------------:"+qian_data_stat_obj.account_coin_count);

       var account_coin_count = Number(qian_data_stat_obj.account_coin_count)-Number(total_coinchange_count)+Number(total_coinreturn_count);

       //let  data_stat_obj = await this.model('data_stat').where({stat_date_str:array2[i]}).find();
       await this.model('data_stat').where({stat_date_str:array2[i]}).update({total_coinchange_count:total_coinchange_count,total_coinreturn_count:total_coinreturn_count,account_coin_count:account_coin_count});


      }

      
      console.log('数据统计处理流程  -- 结束!');
  }

};
